<?php
/**
 * ブログ記事一つを表すモデル
 *
 * @group model
 */
class Member extends DataModel
{
    protected $_schema = array(
        'post_id'           => parent::INTEGER,
        'member_id'         => parent::INTEGER,
        'history_id'        => parent::INTEGER,
        'editor_id'         => parent::INTEGER,
        'update_date'       => parent::DATETIME,
        'publish_date'      => parent::DATETIME,
        
        'name'              => parent::TEXT,
        'note'              => parent::TEXT,
        'editor_note'       => parent::TEXT,
        
        'birth_day'         => parent::DATETIME,
        'avatar'            => parent::TEXT,
        
        'level'             => parent::INTEGER,
        'login_pass'        => parent::TEXT,
        'login_mail'        => parent::TEXT,
        'is_active'         => parent::BOOLEAN,
        
        'login_date'        => parent::DATETIME,
        
        'remote_ip'  => parent::TEXT,
        'user_agent' => parent::TEXT,
        /*
        
        'categories' => parent::TEXT,
        
        
        
         * 
         */
    );


    function isValid(Array $error = null)
    {
        if (isset($this->_login_form)) {

            $val = $this->login_mail;
            if (static::checkLength($val, 128, 4) || static::checkMail($val)) {
                $error['login_mail'] = '4文字以上32文字以下のメールアドレス';
            }
            
            $val = $this->login_pass;
            if (static::checkLength($val, 32, 4)) {
                $error['login_pass'] = '4文字以上32文字以下';
            }
        }
        
        if (isset($this->_edit_member) || isset($this->_edit_profile)) {

            if (!isset($this->birth_day)) {
                $val = sprintf("%04d",@$this->_birthday['Year']) . '-'
                     . sprintf("%02d",@$this->_birthday['Month']) . '-'
                     . sprintf("%02d",@$this->_birthday['Day']);
                $this->birth_day = $val;
                if (static::checkTrueDay($val) || static::checkFuture($val)) {
                    $error['birth_day'] = '不正な値';
                }
            }

            $val = $this->name;
            if (static::checkLength($val, 32, 1)) {
                $error['name'] = '1文字以上32文字以下';
            }

            $val = $this->note;
            echo $val;
            if (static::checkLength($val, 1024, 0)) {
                $error['note'] = '1024文字以内';
            }
        }
        if (isset($this->_edit_member)) {
            
            $val = $this->post_id;
            if (static::checkNumber($val, 1)) {
                $error['post_id'] = '不正な値';
            }
            
            $val = $this->member_id;
            if (static::checkNumber($val, 1)) {
                $error['member_id'] = '不正な値';
            }

            $val = $this->level;
            if (static::checkLimitedCharacters($val, unserialize(USER_LEVEL_PARAMS))) {
                $error['level'] = '不正な値';
            }

            $val = $this->is_active;
            if (static::checkLimitedCharacters($val, array(0,1))) {
                $error['is_active'] = '不正な値';
            }


            $val = $this->login_mail;
            if (static::checkLength($val, 128, 4) || static::checkMail($val)) {
                $error['login_mail'] = '4文字以上32文字以下のメールアドレス';
            }
            
            $val = $this->login_pass;
            if (static::checkLength($val, 32, 4)) {
                $error['login_pass'] = '4文字以上32文字以下';
            }

            $val = $this->editor_note;
            if (static::checkLength($val, 1024, 0)) {
                $error['editor_note'] = '1024文字以内';
            }
        }

        return $error;
    }
    
}